﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TWFLOG : UIWFN
    {

        public TWFLOG()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_WFEVENTS();
            this.DataContext = MV;
            InitializeComponent();
            EventDispatcher.Instance.OnMessage += new EventHandler<MessageEventArgs>(OnMessage);
            EventDispatcher.Instance.OnStatusChange += new EventHandler<StatusEventArgs>(OnStatusChange);

        }
        public void OnStatusChange(object sender, StatusEventArgs e)
        {
            switch (e.StatusType)
            {
                case StatusEventType.StartProgress:
                    INFO = e.Info.Details;
                    //this.progressBar.Visibility = Visibility.Visible;
                    //this.progressBar.IsIndeterminate = true;
                    break;
                case StatusEventType.RunningProgress:
                    INFO = e.Info.Details;    
                //this.tbMessage.Text = e.Info.Details;
                    //this.progressBar.Visibility = Visibility.Visible;
                    //this.progressBar.IsIndeterminate = true;
                    break;
                case StatusEventType.UserLogIn:
                    INFO = e.Info.Details;
                    //this.tbUser.Text = e.Info.Details;
                    break;
                case StatusEventType.StopProgress:
                    INFO = e.Info.Details;
                    //this.tbMessage.Text = e.Info.Details;
                    //this.progressBar.Visibility = Visibility.Hidden;
                    //this.progressBar.IsIndeterminate = false;
                    break;
            }
        }
        public void OnMessage(object sender, MessageEventArgs e)
        {
            try
            {
                //add event in the message stack
                MessageInfo msg = e.Info;
                dwData = TBMain.NewRow();
                switch (msg.Type.ToString())
                {
                    case "None":
                        dwData["ORDTYPE"] = 0;
                        break;
                    case "Error":
                        dwData["ORDTYPE"] = 1;
                        break;
                    case "Info":
                        dwData["ORDTYPE"] = 2;
                        break;
                    case "Verbose":
                        dwData["ORDTYPE"] = 3;
                        break;
                    case "Help":
                        dwData["ORDTYPE"] = 4;
                        break;
                    default:
                        dwData["ORDTYPE"] = 0;
                        break;
                }
                MSGTYP = msg.Type.ToString();
                INFO = msg.Details;
                dwData["HOW"] = msg.Type;
                dwData["WFWHEN"] = msg.When;
                dwData["WFSOURCE"] = msg.Details;
                dwData["WHAT"] = Helper.Left(msg.Where.PadRight(800), 800);
                TBMain.Rows.InsertAt(dwData, 0);
                if (e.Info.Type == MessageEventType.Error)
                {
                    if (e.Info.Details.Contains("AGENTRECHECK"))
                    {
                        app.CheckConnection();
                        app.Connected = false;
                        MessageBox.Show("请重新登陆报表程序");
                        return;
                    }
                    ((VICC_WFEVENTS)MV).CheckEvents("A999001", null, e.Info.What, e.Info);
                }
            }
            catch (Exception ex)
            { }
        }

        private void BtnClear_Click(object sender, RoutedEventArgs e)
        {
            vwMaster.View.MoveCurrentTo(-1);
            TBMain.Clear();
        }
        public override void LOADBYAUF()
        {
            base.LOADBYAUF();
            if (ORDERNUM == "0")
            {
                cvMaster.CustomFilter = null;
            }
            else
            {
                cvMaster.CustomFilter = "ORDTYPE='" + ORDERNUM + "'";
            }
        }
    }
}
